import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Controls 2.12
import QtQuick.Layouts 1.12

ApplicationWindow {
    id: mainApplicationWindow
    width: 1400
    height: 900
    visible: true
    title: qsTr("甘蔗农机智能管控平台 V1.0")
    color: "#f5f7fa"
    
    // 信号定义
    signal logoutRequested()
    
    // 当前页面状态
    property string currentPage: "login"
    property bool animating: false
    
    // 初始化处理
    Component.onCompleted: {
        // 如果已经登录，直接显示主界面
        if (authManager.isLoggedIn) {
            currentPage = "main"
            loadMainInterface()
        }
    }
    
    // 主 StackView 管理所有页面
    StackView {
        id: mainStackView
        anchors.fill: parent
        
        // 根据登录状态决定初始页面
        initialItem: authManager.isLoggedIn ? mainInterfaceComponent : loginPageComponent
        
        // 页面切换动画
        pushEnter: Transition {
            PropertyAnimation {
                property: "x"
                from: mainApplicationWindow.width
                to: 0
                duration: 300
                easing.type: Easing.OutCubic
            }
        }
        
        pushExit: Transition {
            PropertyAnimation {
                property: "x"
                from: 0
                to: -mainApplicationWindow.width
                duration: 300
                easing.type: Easing.OutCubic
            }
        }
        
        popEnter: Transition {
            PropertyAnimation {
                property: "x"
                from: -mainApplicationWindow.width
                to: 0
                duration: 300
                easing.type: Easing.OutCubic
            }
        }
        
        popExit: Transition {
            PropertyAnimation {
                property: "x"
                from: 0
                to: mainApplicationWindow.width
                duration: 300
                easing.type: Easing.OutCubic
            }
        }
    }
    
    // 登录页面组件
    Component {
        id: loginPageComponent
        
        Loader {
            id: loginLoader
            source: "qrc:/pages/LoginPage.qml"
            
            onLoaded: {
                if (item) {
                    console.log("LoginPage loaded, connecting signals")
                    item.loginSuccess.connect(function () {
                        console.log("Login success signal received, switching to main interface")
                        currentPage = "main"
                        loadMainInterface()
                    })
                    
                    item.switchToRegister.connect(function () {
                        if (!animating) {
                            animating = true
                            currentPage = "register"
                            mainStackView.push(registerPageComponent)
                            animationTimer.start()
                        }
                    })
                }
            }
        }
    }
    
    // 注册页面组件
    Component {
        id: registerPageComponent
        
        Loader {
            id: registerLoader
            source: "qrc:/pages/RegisterPage.qml"
            
            onLoaded: {
                if (item) {
                    console.log("RegisterPage loaded, connecting signals")
                    
                    // 连接跳转到登录页信号
                    item.switchToLogin.connect(function () {
                        console.log("switchToLogin signal received in MainWindow")
                        if (!animating) {
                            animating = true
                            currentPage = "login"
                            console.log("Popping to login page")
                            mainStackView.pop()
                            animationTimer.start()
                        } else {
                            console.log("Animation in progress, ignoring switchToLogin")
                        }
                    })
                }
            }
        }
    }
    
    // 主界面组件
    Component {
        id: mainInterfaceComponent
        
        Loader {
            id: mainInterfaceLoader
            source: "qrc:/main.qml"
            
            onLoaded: {
                console.log("Main interface loaded successfully")
                
                // 连接退出登录信号
                if (item && item.logoutRequested) {
                    item.logoutRequested.connect(function() {
                        console.log("Logout requested, returning to login page")
                        // 退出登录后返回到登录页面
                        currentPage = "login"
                        authManager.logoutUser()
                        
                        // 检查StackView深度，如果只有一层则直接替换，否则pop
                        if (mainStackView.depth > 1) {
                            mainStackView.pop()
                        } else {
                            mainStackView.replace(loginPageComponent)
                        }
                    })
                }
            }
        }
    }
    
    // 动画状态控制定时器
    Timer {
        id: animationTimer
        interval: 350
        onTriggered: animating = false
    }
    
    // 键盘快捷键
    Shortcut {
        sequence: "Escape"
        onActivated: {
            if (mainStackView.depth > 1) {
                mainStackView.pop()
                currentPage = "login"
            }
        }
    }
    
    // 加载主界面函数
    function loadMainInterface() {
        console.log("loadMainInterface called, animating:", animating)
        if (!animating) {
            animating = true
            currentPage = "main"
            console.log("Pushing main interface component")
            
            // 使用平滑过渡效果切换到主界面
            mainStackView.push(mainInterfaceComponent, {}, StackView.Transition)
            
            // 延迟一段时间后停止动画状态
            loadMainTimer.start()
        }
    }
    
    // 主界面加载定时器
    Timer {
        id: loadMainTimer
        interval: 500
        onTriggered: animating = false
    }
    
    // 关闭窗口时的清理
    onClosing: {
        // 清理各个页面的状态
        if (mainStackView.currentItem && typeof mainStackView.currentItem.clearForm === "function") {
            mainStackView.currentItem.clearForm()
        }
        
        // 清除登录状态
        console.log("窗口关闭，清除登录状态")
        authManager.clearLoginState()
    }
}
